define(function(){
	// checkbox
	var SelectInput=function (initConfig){
		var selectInput=this;
		selectInput.config={};


		var keyupBlur=false;

		selectInput.config.inputStyle=$.getDefined(initConfig.inputStyle,false);
		selectInput.config.placeholder=$.getDefined(initConfig.placeholder,false);


		selectInput.optionDataList={};
		selectInput.optionDataValueArr=[];
		selectInput.optionShowDataList=[];
		selectInput.inputClickComplete=function(){};
		selectInput.inputKeyupComplete=function(){};
		selectInput.inputBlurComplete=function(){};

		selectInput.optionClickComplete=function(){};



		selectInput.inputClick=function(){
			selectInput.inputClickComplete();
		}
		selectInput.inputKeyup=function(e){
			selectInput.inputKeyupComplete(e);
		}
		selectInput.inputBlur=function(){
			selectInput.inputBlurComplete();
		}

		selectInput.optionClick=function(){
			selectInput.optionClickComplete();
		}

		selectInput.optionDataUpdate=function(){};



		selectInput.onChange=$.getDefined(initConfig.onChange,function(){});

		selectInput.config.optionShow=false;
		selectInput.config.selected=$.getDefined(initConfig.selected,'');
		selectInput.config.optionDataList=$.getDefined(initConfig.optionList,false);

		selectInput.config.numberOption=$.getDefined(initConfig.numberOption,false);
		if(selectInput.config.numberOption!==false){
			selectInput.config.numberOption.min=$.getDefined(selectInput.config.numberOption.min,0);
			selectInput.config.numberOption.max=$.getDefined(selectInput.config.numberOption.max,0);
			selectInput.config.numberOption.interval=$.getDefined(selectInput.config.numberOption.interval,1);
			selectInput.config.numberOption.contextFormat=$.getDefined(selectInput.config.numberOption.contextFormat,function(v){return v});
		}

		selectInput.config.searchOption=$.getDefined(initConfig.searchOption,false);



		selectInput.$dom=$('<div class="select"></div>');
		selectInput.$input=$('<input type="text" class="form-control" />').appendTo(selectInput.$dom);
		selectInput.$searchBtn=$('<input type="button" class="searchBtn" />').hide().appendTo(selectInput.$dom);
		selectInput.$optionList=$('<ul class="optionList"></ul>').appendTo(selectInput.$dom);




		selectInput.value=function (sVal){
			if(sVal!==undefined){
				if(sVal===''){
					selectInput.inputReset();
					return true;
				}
				var selectedData=selectInput.optionDataList[sVal];
				if(selectedData===undefined){
					selectInput.inputReset();
					return false;
				}else{
					//selectedDom.$dom.addClass('selected').siblings().removeClass('selected');
					selectInput.$input.val(selectedData.context);
					selectInput.$input.attr('data-value',selectedData.dataValue);
					selectInput.config.selected=selectedData.dataValue;
					return true;
				}
			}else{
				return selectInput.config.selected;
			}
		};
		selectInput.inputReset=function(){
			selectInput.optionShow(false);
			selectInput.config.selected='';
			selectInput.$input.valueReset();
		}
		selectInput.inputCheck=function(){
			var inputStr=selectInput.$input.val();
			if(inputStr==='' || selectInput.config.selected===''){
				if(inputStr===selectInput.config.selected){
					return true;
				}else{
					return false;
				}
			}
			var selectedOptionData=selectInput.optionDataList[selectInput.config.selected];
			if(selectedOptionData===undefined){
				return false;
			}
			return inputStr===selectedOptionData.context;
		}



		selectInput.selected=function(sVal){
			var selectedResult=selectInput.value(sVal);
			if(selectedResult===true){
				selectInput.$input.change();
				selectInput.onChange();
			}
			return selectedResult;
		}


		selectInput.optionUpdate=function(arr){
			selectInput.optionDataValueArr=[];
			selectInput.optionDataList={};
			$.each(arr, function(i,optionData) {
				selectInput.optionDataValueArr.push(optionData.dataValue);
				selectInput.optionDataList[optionData.dataValue]=optionData;
			});
		}
		function inputSelectedSibling(interval){
			var selectedIndex=selectInput.optionDataValueArr.indexOf(selectInput.config.selected);
			selectedIndex+=interval;
			if(selectedIndex>=selectInput.optionDataValueArr.length){
				selectedIndex=selectInput.optionDataValueArr.length-1;
			}
			if(selectedIndex<0){
				selectedIndex=0;
			}
			selectInput.selected(selectInput.optionDataValueArr[selectedIndex]);
			selectInput.optionShow(false);
		}
		function optionFilter(keyword){
			var resultArr=[];
			var $options=selectInput.$optionList.children('[data-value]');
			$options.show();
			if(keyword!==''){
				$.each(selectInput.optionDataList, function(dataValue,optionData) {
					if(optionData.context.indexOf(keyword)>=0){
						resultArr.push(optionData);
					}
				});
				if(resultArr.length>=0){
					return resultArr;
				}else{
					return [];
				}
			}else{
				return selectInput.optionDataList;
			}
		}

		selectInput.arrToOpitonShowData=function(arr){
			var optionListArr=[];
			$.each(selectInput.optionDataValueArr, function(i,dataValue) {
				var optionData=arr[dataValue];
				if(optionData!==undefined){
					optionListArr.push({
						text:optionData.context,
						callback:{
							mousedown:function(){
								selectInput.optionClick();
							},
							click:function(){
								selectInput.selected(optionData.dataValue);
							}
						},
						data:optionData
					});
				}


			});
			return optionListArr
		}
		selectInput.optionShow=function(optionArr){
			if(selectInput.popUpBox!=undefined){
				selectInput.popUpBox.close();
			}
			if(optionArr===undefined){
				selectInput.optionShowDataList=selectInput.arrToOpitonShowData(selectInput.optionDataList)
			}else{
				if(optionArr===false){
					return false;
				}else{
					if(typeof optionArr ==='string'){
						selectInput.popUpBox=new PopUpBox({
							type:'bubble',
							style:'',
							show:true,
							positionAbsolute:true,
							content:$('<div style="padding:2px 4px"></div>').text(optionArr),
							follow:{
								$dom:selectInput.$input,
								direction:'center bottom'
							},
							bubbleArrow:'auto',
							position:{
								//'margin-left':-74
							}
						});
						return true;
					}
					if(optionArr.length<1){
						return false;
					}else{
						selectInput.optionShowDataList=selectInput.arrToOpitonShowData(optionArr);
					}
				}
			}
			selectInput.popUpBox=new PopUpBox({
				type:'menu',
				style:'font-size-12 text-left',
				show:true,
				//$mouseLeaveClose:selectInput.$input,
				positionAbsolute:true,
				content:selectInput.optionShowDataList,
				focus:false,
				//hideScrollBar:false,
				//scrollFixed:false,
				follow:{
					$dom:selectInput.$input,
					direction:'center bottom'
				}
			});
		}


		selectInput.$input.on('keyup',function(e){
			selectInput.inputKeyup(e);
		});
		selectInput.$input.on('blur',function(){
			selectInput.inputBlur();
		});
		selectInput.$input.on('click',function(){
			selectInput.inputClick();
		});


		if(selectInput.config.inputStyle!==false){
			selectInput.$input.css(selectInput.config.inputStyle);
		}
		if(selectInput.config.placeholder!==false){
			selectInput.$input.attr('data-tips',selectInput.config.placeholder);
			if(selectInput.$input.val()===''){
				selectInput.$input.val(selectInput.config.placeholder);
			}
		}

		var inputKeyupCompleteTimeout,inputBlurCompleteTimeout,optionDomMousedown=false;
		var optionShowFrom=false;


		function dataOptionInit(){
			selectInput.$input.addClass('selectInput');



			selectInput.optionDataUpdate=function(newData){
				if(newData!==undefined){
					selectInput.config.optionDataList=newData;
				}
				selectInput.optionUpdate(selectInput.config.optionDataList);
			}
			selectInput.optionDataUpdate();

			selectInput.config.inputedTimeout=$.getDefined(initConfig.inputKeyupCompleteTimeout,0);
			selectInput.inputClickComplete=function(){
				selectInput.$input.select();
				selectInput.optionShow();
			}
			selectInput.inputKeyupComplete=function(e){
				//console.log(e.keyCode)
				switch(e.keyCode){
					case 9:
						selectInput.inputClickComplete();
						return false;
						break;
					case 38:
						inputSelectedSibling(-1);
						return false;
						break;
					case 40:
						inputSelectedSibling(1);
						return false;
						break;
				}
				clearTimeout(inputKeyupCompleteTimeout);
				inputKeyupCompleteTimeout=setTimeout(function(){
					var keyword=selectInput.$input.val();
					selectInput.optionShow(optionFilter(keyword));
				},selectInput.config.inputedTimeout);

			}



			selectInput.inputBlurComplete=function(){
				clearTimeout(inputKeyupCompleteTimeout);
				clearTimeout(inputBlurCompleteTimeout);
				inputBlurCompleteTimeout=setTimeout(function(){
					var possibleValue;
					var inputFinish=selectInput.inputCheck();
					if(inputFinish===false){
						if(selectInput.optionShowDataList.length>0){
							possibleValue=selectInput.optionShowDataList[0].data.dataValue;
						}else{
							possibleValue=selectInput.config.selected;
						}
						selectInput.value(possibleValue);
					}
					selectInput.optionShow(false);
				},100);
			}

			selectInput.optionClickComplete=function(){
				setTimeout(function(){
					clearTimeout(inputBlurCompleteTimeout);
				},50);
			}
		}

		if(selectInput.config.optionDataList!==false){
			dataOptionInit();
		}



		function numberOptionInit(){


			selectInput.optionDataUpdate=function(newData){
				if(newData!==undefined){
					selectInput.config.numberOption.min=$.getDefined(newData.min,selectInput.config.numberOption.min);
					selectInput.config.numberOption.max=$.getDefined(newData.max,selectInput.config.numberOption.max);
					selectInput.config.numberOption.interval=$.getDefined(newData.interval,selectInput.config.numberOption.interval);
				}
				var v_min=selectInput.config.numberOption.min,
					v_max=selectInput.config.numberOption.max,
					v_interval=selectInput.config.numberOption.interval;
				var oArr=[];
				for(;v_min<v_max;v_min+=v_interval){
					oArr.push({
						dataValue:v_min,
						context:selectInput.config.numberOption.contextFormat(v_min)
					});
				}
				oArr.push({
					dataValue:v_max,
					context:selectInput.config.numberOption.contextFormat(v_max)
				});

				selectInput.optionUpdate(oArr);
			}
			selectInput.optionDataUpdate();

			selectInput.$input.addClass('selectInput');

			function possibleValueCheck(){
				var possibleValue;
				var inputedVal=selectInput.$input.val();
				var inputFinish=selectInput.inputCheck();
				if(inputFinish===false){
					possibleValue=Math.ceil($.parseInt(inputedVal)/selectInput.config.numberOption.interval)*selectInput.config.numberOption.interval;

					if(possibleValue<selectInput.config.numberOption.min){
						possibleValue=selectInput.config.numberOption.min;
					}
					if(possibleValue>selectInput.config.numberOption.max){
						possibleValue=selectInput.config.numberOption.max;
					}
					selectInput.value(possibleValue);
				}
			}

			selectInput.inputClickComplete=function(){
				selectInput.$input.select();
				selectInput.optionShow();
			}
			selectInput.inputKeyupComplete=function(e){
				switch(e.keyCode){
					case 9:
						selectInput.inputClickComplete();
						return false;
						break;
					case 38:
						inputSelectedSibling(-1);
						return false;
						break;
					case 40:
						inputSelectedSibling(1);
						return false;
						break;
				}
			}
			selectInput.inputBlurComplete=function(){
				clearTimeout(inputBlurCompleteTimeout);
				inputBlurCompleteTimeout=setTimeout(function(){
					possibleValueCheck();
					selectInput.optionShow(false);
				},100);
			}

			selectInput.optionClickComplete=function(){
				setTimeout(function(){
					clearTimeout(inputBlurCompleteTimeout);
				},50);
			}
			selectInput.$input.on('change',function(){
				possibleValueCheck();
			});
		}
		if(selectInput.config.numberOption!==false){
			numberOptionInit();
		}



		function searchOptionInit(){


			selectInput.optionDataUpdate=function(newData){
				if(newData!==undefined){
					selectInput.config.searchOption=newData;
				}
			}

			selectInput.$dom.addClass('searchBar');
			selectInput.$input.addClass('searchInput');
			selectInput.$searchBtn.show();

			selectInput.config.inputedTimeout=$.getDefined(initConfig.inputKeyupCompleteTimeout,2000);
			selectInput.inputClickComplete=function(){
				selectInput.$input.select();
			}
			var searching=false;
			selectInput.inputKeyupComplete=function(e,dtime){
				if(e!==undefined){
					switch(e.keyCode){
						case 9:
							selectInput.inputClickComplete();
							return false;
							break;
						case 13:
							setTimeout(function(){
								clearTimeout(inputBlurCompleteTimeout);
								selectInput.inputKeyupComplete(undefined,0);
							},50);
							return false;
							break;
					}
				}
				clearTimeout(inputKeyupCompleteTimeout);

				if(dtime===undefined){
					dtime=selectInput.config.inputedTimeout;
				}

				inputKeyupCompleteTimeout=setTimeout(function(){
					var keyword=selectInput.$input.value();
					if(keyword!==''){
						selectInput.optionShow('搜索中...');
						if(searching===true){
							return false;
						}else{
							searching=true;
						}
						selectInput.config.searchOption(keyword,function(arr){
							selectInput.optionUpdate(arr);
							if(arr.length>0){
								selectInput.optionShow();
							}else{
								selectInput.optionShow('未搜索到结果');
							}
							searching=false;
						});
					}else{
						selectInput.inputReset();
						return true;
					}
				},dtime);
			}

			selectInput.inputBlurComplete=function(){
				clearTimeout(inputKeyupCompleteTimeout);
				clearTimeout(inputBlurCompleteTimeout);
				inputBlurCompleteTimeout=setTimeout(function(){
					var possibleValue;
					var inputFinish;
					var inputValue=selectInput.$input.value();
					if(inputValue===''){
						selectInput.inputReset();
					}else{
						inputFinish=selectInput.inputCheck();
						if(inputFinish===false){
							if(selectInput.optionShowDataList.length>0){
								possibleValue=selectInput.optionShowDataList[0].data.dataValue;
							}else{
								possibleValue='';
							}
							selectInput.selected(possibleValue);
						}
					}
					selectInput.optionShow(false);
				},100);
			}
			selectInput.optionClickComplete=function(){
				setTimeout(function(){
					clearTimeout(inputBlurCompleteTimeout);
				},50);
			}
			selectInput.$searchBtn.on('click',function(){
				setTimeout(function(){
					clearTimeout(inputBlurCompleteTimeout);
					selectInput.inputKeyupComplete(undefined,0);
				},50);
			});

			selectInput.$input.on('keyup',function(e){
				if(selectInput.$input.val()===''){
					selectInput.inputReset();
					selectInput.$input.val('');
					selectInput.optionShow(false);
				}else{
					selectInput.optionShow('搜索中...');
				}
			});

		}
		if(selectInput.config.searchOption!==false){
			searchOptionInit();
		}




		/*
		selectInput.$dom.on('mouseleave',function(){
			//selectInput.optionShow(false);
		});*/
		if(selectInput.config.selected!==''){
			selectInput.value(selectInput.config.selected);
		}else{
			if(selectInput.optionDataValueArr.length>0){
				selectInput.value(selectInput.optionDataValueArr[0]);
			}
		}




	}

	window.SelectInput=SelectInput;
	return SelectInput;
})
